<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue.direcitve</title>
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <script type="text/javascript" src="../assets/js/vue.js"></script>
</head>
<body>
    <h1>vue.direcitve自定义指令</h1>
    <hr>
    <div id="app">
        <div v-chen="color">{{num}}</div>
        <p><button @click="add">Add</button></p>
    </div>
    <p>
        <button onclick="unbind()">解绑</button>
    </p>
    <script type="text/javascript">
         function unbind(){//解绑
             app.$destroy();
         }
    //全局API，构造器外定义新功能，Vue.directive用于定义标签里的一个属性
    //自定义指令由五个生命周期（亦称钩子函数），bind,inserted,update,componentUpdated,unbind
        Vue.directive("chen",{
            bind:function(el,binding){//被绑定
                console.log('1 - bind');
                el.style="color:"+binding.value;
            },
            inserted:function(){//绑定到节点
                console.log('2 - inserted');
            },
            update:function(){//组件更新
                console.log('3 - update');
            },
            componentUpdated:function(){//组件更新完成
                console.log('4 - componentUpdated');
            },
            unbind:function(){//解绑
                console.log('5 - unbind');
            }}
           )
        // el.style="color:"+binding.value;
        var app=new Vue({//构造器
            el:'#app',
            data:{//变量
                num:10,
                color:"blue"
            },
            methods:{//方法
                add:function(){
                    this.num++;
                }
            }
        }) 
    </script>
</body>
</html>